﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace NPSoft.Ferrero.Framework.DAL
{
    public sealed partial class SqlHelper
    {
        #region "读取数据"

        /// <summary>
        /// 取得Int值
        /// </summary>
        /// <param name="row">记录</param>
        /// <param name="column">字段名称</param>
        /// <param name="value">缺省值</param>
        /// <returns>返回int值</returns>
        public static int GetInt(DataRow row, string column, int value)
        {
            if (Convert.IsDBNull(row[column]))
                return value;
            else
                return Convert.ToInt32(row[column]);
        }

        /// <summary>
        /// 取得Single值
        /// </summary>
        /// <param name="row">记录</param>
        /// <param name="column">字段名称</param>
        /// <param name="value">缺省值</param>
        /// <returns>返回Single值</returns>
        public static Single GetSingle(DataRow row, string column, Single value)
        {
            if (Convert.IsDBNull(row[column]))
                return value;
            else
                return Convert.ToSingle(row[column]);
        }

        /// <summary>
        /// 获得Long值
        /// </summary>
        /// <param name="row">记录</param>
        /// <param name="column">字段名称</param>
        /// <param name="value">缺省值</param>
        /// <returns>返回Long值</returns>
        public static long GetLong(DataRow row, string column, long value)
        {
            if (Convert.IsDBNull(row[column]))
                return value;
            else
                return Convert.ToInt64(row[column]);
        }

        /// <summary>
        /// 取得Decimal值
        /// </summary>
        /// <param name="row">记录</param>
        /// <param name="column">字段名称</param>
        /// <param name="value">缺省值</param>
        /// <returns>返回Decimal值</returns>
        public static decimal GetDecimal(DataRow row, string column, decimal value)
        {
            if (Convert.IsDBNull(row[column]))
                return value;
            else
                return Convert.ToDecimal(row[column]);
        }

        /// <summary>
        /// 取得byte值
        /// </summary>
        /// <param name="row">记录</param>
        /// <param name="column">字段名称</param>
        /// <param name="value">缺省值</param>
        /// <returns>返回byte值</returns>
        public static byte Getbyte(DataRow row, string column, byte value)
        {
            if (Convert.IsDBNull(row[column]))
                return value;
            else
                return Convert.ToByte(row[column]);
        }

        /// <summary>
        /// 取得byte[]
        /// </summary>
        /// <param name="row">记录</param>
        /// <param name="column">字段名称</param>
        /// <returns>返回byte[]</returns>
        public static byte[] Getbytes(DataRow row, string column)
        {
            if (Convert.IsDBNull(row[column]))
                return null;
            else
                return (byte[])row[column];
        }

        /// <summary>
        /// 取得byte[]
        /// </summary>
        /// <param name="row">记录</param>
        /// <param name="column">字段名称</param>
        /// <param name="value">缺省值</param>
        /// <returns>返回byte[]</returns>
        public static byte[] Getbytes(DataRow row, string column, byte[] value)
        {
            if (Convert.IsDBNull(row[column]))
                return value;
            else
                return (byte[])row[column];
        }

        /// <summary>
        /// 取得Guid值
        /// </summary>
        /// <param name="row">记录</param>
        /// <param name="column">字段名称</param>
        /// <returns>返回Guid值</returns>
        public static Guid GetGuid(DataRow row, string column)
        {
            if (Convert.IsDBNull(row[column]))
                return Guid.Empty;
            else
                return new Guid(Convert.ToString(row[column]));
        }

        /// <summary>
        /// 取得Guid值
        /// </summary>
        /// <param name="row">记录</param>
        /// <param name="column">字段名称</param>
        /// <param name="value">缺省值</param>
        /// <returns>返回Guid值</returns>
        public static Guid GetGuid(DataRow row, string column, Guid value)
        {
            if (Convert.IsDBNull(row[column]))
                return value;
            else
                return new Guid(Convert.ToString(row[column]));
        }

        /// <summary>
        /// 取得DateTime值
        /// </summary>
        /// <param name="row">记录</param>
        /// <param name="column">字段名称</param>
        /// <returns>返回DateTime值</returns>
        public static DateTime GetDateTime(DataRow row, string column)
        {
            if (Convert.IsDBNull(row[column]))
                return DateTime.MinValue;
            else
                return Convert.ToDateTime(row[column]);
        }

        /// <summary>
        /// 取得DateTime值
        /// </summary>
        /// <param name="row">记录</param>
        /// <param name="column">字段名称</param>
        /// <param name="value">缺省值</param>
        /// <returns>返回DateTime值</returns>
        public static DateTime GetDateTime(DataRow row, string column, DateTime value)
        {
            if (Convert.IsDBNull(row[column]))
                return value;
            else
                return Convert.ToDateTime(row[column]);
        }

        /// <summary>
        /// 取得bool值
        /// </summary>
        /// <param name="row">记录</param>
        /// <param name="column">字段名称</param>
        /// <returns>返回bool值</returns>
        public static bool GetBool(DataRow row, string column)
        {
            if (Convert.IsDBNull(row[column]))
                return false;
            else
            {
                string s = Convert.ToString(row[column]).ToLower();
                return (s == "1" || s == "true");
            }
        }

        /// <summary>
        /// 取得bool值
        /// </summary>
        /// <param name="row">记录</param>
        /// <param name="column">字段名称</param>
        /// <param name="value">缺省值</param>
        /// <returns>返回bool值</returns>
        public static bool GetBool(DataRow row, string column, bool value)
        {
            if (Convert.IsDBNull(row[column]))
                return value;
            else
            {
                string s = Convert.ToString(row[column]).ToLower();
                return (s == "1" || s == "true");
            }
        }

        /// <summary>
        /// 取得string值
        /// </summary>
        /// <param name="row">记录</param>
        /// <param name="column">字段名称</param>
        /// <returns>返回string值</returns>
        public static string GetString(DataRow row, string column)
        {
            if (Convert.IsDBNull(row[column]))
                return "";
            else
                return Convert.ToString(row[column]);
        }

        /// <summary>
        /// 取得string值
        /// </summary>
        /// <param name="row">记录</param>
        /// <param name="column">字段名称</param>
        /// <param name="value">缺省值</param>
        /// <returns>返回string值</returns>
        public static string GetString(DataRow row, string column, string value)
        {
            if (Convert.IsDBNull(row[column]))
                return value;
            else
                return Convert.ToString(row[column]);
        }

        #endregion
    }
}
